#include "DBFileStream.h"

namespace db
{
    
// ----------------------------------- WriteFileStream ----------------------------------------

WriteFileStream::WriteFileStream(const std::string& fileName)
{
        fopen_s(&m_file, fileName.c_str(), "w");
}

bool WriteFileStream::write(const void* data, const unsigned int size)
{
    if (m_file)
    {
        return fwrite(data, 1, size, m_file) > 0;
    }
    return false;
}

void WriteFileStream::complete()
{
    if (m_file)
    {
        fclose(m_file);
    }
}

bool WriteFileStream::open()
{
    return m_file ? true : false;
}

// ----------------------------------- ReadFileStream ----------------------------------------

ReadFileStream::ReadFileStream(const std::string& fileName)
{
        fopen_s(&m_file, fileName.c_str(), "r");
}

ReadFileStream::~ReadFileStream()
{
    if (m_file)
    {
        fclose(m_file);
    }
}

bool ReadFileStream::read(void* data, const unsigned int size)
{
    if (m_file)
    {
        return fread(data, 1, size, m_file) > 0;
    }
    return false;
}

void ReadFileStream::complete()
{
    if (m_file)
    {
        fclose(m_file);
    }
}

bool ReadFileStream::open()
{
    return m_file ? true : false;
}

};